package cn.com.alibaba.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import cn.com.alibaba.dao.UserDao;
import cn.com.alibaba.dao.impl.UserDaoImpl;
import cn.com.alibaba.domain.User;
import cn.com.alibaba.service.UserService;
import cn.com.alibaba.utils.JdbcUtils;
import cn.com.alibaba.utils.MD5Utils;

public class UserServiceImpl implements UserService {

	private UserDao ud = new UserDaoImpl();
	
	public User login(User user) {
		JdbcUtils.getConnectionWithTransaction();
		String loginName = user.getLoginName();
		String loginPwd = MD5Utils.md5(user.getLoginPwd());
		System.out.println(loginName);
		System.out.println(loginPwd);
		User u;//u是数据库中查询到的
		try {
			u = ud.getUserByLogin(loginName, loginPwd);
		} catch (Exception e) {
			e.printStackTrace();
			JdbcUtils.rollback();
			throw new RuntimeException(e.getMessage());
		}
		JdbcUtils.commit();
		return u;
	}

	public List<User> getUserListByCondition(User u) {
		JdbcUtils.getConnectionWithTransaction();
		try {
			StringBuilder builder = new StringBuilder();
			List<Object> params = new ArrayList<Object>();
			if(u.getUserName() != null){
				builder.append(" and  userName like ? ");
				params.add("%"+u.getUserName()+"%");
			}
			if(StringUtils.isNotBlank(u.getSex())){
				builder.append(" and  sex = ? ");
				params.add(u.getSex());
			}
			if(StringUtils.isNotBlank(u.getEducation())){
				builder.append(" and  education = ? ");
				params.add(u.getEducation());
			}
			if("1".equals(u.getIsUpload())){
				builder.append(" and filename is not null");
			}else if("0".equals(u.getIsUpload())){
				builder.append(" and filename is null");
			}
			String condition = builder.toString();
			Object[] parms = params.toArray();
			JdbcUtils.commit();
			return ud.getUserlistByCondition(condition, parms);
		} catch (Exception e) {
			e.printStackTrace();
			JdbcUtils.rollback();
			throw new RuntimeException(e.getMessage());
		}
	}

	public void saveUser(User user) {
		JdbcUtils.getConnectionWithTransaction();
		try {
			user.setLoginPwd(MD5Utils.md5(user.getLoginPwd()));
			ud.addUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			JdbcUtils.rollback();
			throw new RuntimeException(e.getMessage());
		}
		JdbcUtils.commit();
	}

	public User getUserByUserId(Integer id) {
		JdbcUtils.getConnectionWithTransaction();
		User u;//u是数据库中查询到的
		try {
			u = ud.getUserByUserId(id);
		} catch (Exception e) {
			e.printStackTrace();
			JdbcUtils.rollback();
			throw new RuntimeException(e.getMessage());
		}
		JdbcUtils.commit();
		return u;
	}

	public void deleteUserByUserId(Integer id) {
		JdbcUtils.getConnectionWithTransaction();
		try {
			ud.deleteUserByUserId(id);
		} catch (Exception e) {
			e.printStackTrace();
			JdbcUtils.rollback();
			throw new RuntimeException(e.getMessage());
		}
		JdbcUtils.commit();
		
	}

	public void updateUser(User user) {
		JdbcUtils.getConnectionWithTransaction();
		try {
			user.setLoginPwd(MD5Utils.md5(user.getLoginPwd()));
			ud.updateUser(user);
		} catch (Exception e) {
			e.printStackTrace();
			JdbcUtils.rollback();
			throw new RuntimeException(e.getMessage());
		}
		JdbcUtils.commit();
	}
}
